package com.tech.cloudapp.controller;


import com.tech.cloudapp.biz.service.IBUserService;
import com.tech.cloudapp.db.entity.UserInfo;
import com.tech.cloudapp.exception.BusinessException;
import com.tech.cloudapp.model.CommonResponseDO;
import com.tech.cloudapp.po.AuthPO;
import com.tech.cloudapp.po.UserInfoPO;
import com.tech.cloudapp.util.BeanCopyUtils;
import com.tech.cloudapp.util.ValidatorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author mojianyou
 * @since 2025-03-23
 */
@RestController
@RequestMapping("/user-info")
public class UserInfoController {

    @Autowired
    private IBUserService bUserServiceImpl;

    @PostMapping(value = "auth")
    public CommonResponseDO auth(@RequestBody AuthPO authPO) throws BusinessException {
        ValidatorUtils.validate(authPO);
        String token = bUserServiceImpl.getToken(authPO.getUserId());
        return CommonResponseDO.success(token);
    }

    @PostMapping(value = "register")
    public CommonResponseDO register(@RequestBody UserInfoPO userInfoPO) throws BusinessException {
        ValidatorUtils.validate(userInfoPO,UserInfoPO.UserRegister.class);
        UserInfo userInfo = BeanCopyUtils.copyBean(userInfoPO,UserInfo.class);
        String result =  bUserServiceImpl.register(userInfo);
        return CommonResponseDO.success(result);
    }

    @PostMapping(value = "isexists")
    public CommonResponseDO isexists(@RequestBody UserInfoPO userInfoPO)
    {
        ValidatorUtils.validate(userInfoPO, UserInfoPO.UserQuery.class);
        UserInfo userInfo = BeanCopyUtils.copyBean(userInfoPO,UserInfo.class);
        int status = bUserServiceImpl.vaildate(userInfo);
        return CommonResponseDO.success(status);
    }

    @PostMapping(value = "update")
    public CommonResponseDO update(@RequestBody UserInfo userInfoPO) throws BusinessException {
        ValidatorUtils.validate(userInfoPO,UserInfoPO.UserUpdate.class);
        UserInfo userInfo = BeanCopyUtils.copyBean(userInfoPO,UserInfo.class);
        bUserServiceImpl.update(userInfo);
        return CommonResponseDO.success();
    }



}

